package cn.liutao.auth.api;

import cn.liutao.auth.entity.param.ExchangeParam;
import cn.liutao.auth.service.UserService;
import cn.liutao.common.entity.LoginUser;
import cn.liutao.common.utils.AuthUtils;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.common.utils.ValidUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;
import java.io.IOException;

/**
 * @author: liutao
 * @description: 充值接口
 * @date: 2023/12/1 9:24
 */
@WebServlet("/user/exchange")
public class ExchangeServlet extends HttpServlet {
    private UserService userService;

    @Override
    public void init() throws ServletException {
        userService = UserService.getInstance();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AuthUtils.isAdmin();
        ExchangeParam exchangeParam = ServletUtils.getBodyParam(req, ExchangeParam.class);
        ValidUtil.validated(exchangeParam, Default.class);
        Integer userId = LoginUser.getUserId();
        userService.exchange(userId,exchangeParam);
        ServletUtils.ok(resp);
    }


}
